<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class IntegralController extends Controller
{
    // 获取订单列表
    public function get_orders(){
        $order_service = new IntegralOrderService();
        $list = $order_service->getOrders()['data'];
        return $this->success(new IntegralOrderCollection($list));
    }

    // 获取订单详情
    public function get_order_info($id){
        $order_service = new IntegralOrderService();
        $rs = $order_service->getOrderInfoById($id,'user');
        return $rs['status']?$this->success(new IntegralOrderResource($rs['data']),$rs['msg']):$this->error($rs['msg']);
    }

    public function index(Request $request){
  
        if(empty(request('class_id'))){
            $data = $this->getService('IntegralGoods', true)->where('goods_status', 1)->where('is_recommend', 1)->take(4)->orderBy('goods_sale', 'desc')->paginate(request('per_page'));
        }else{
            $data = $this->getService('IntegralGoods', true)->where('goods_status', 1)->where('cid',request('class_id'))->orderBy('goods_sale', 'desc')->paginate(request('per_page'));
        }

        return $this->success($data);
    }


    // 获取积分分类
    public function get_integral_class(){
        return $this->success($this->getService('IntegralGoodsClass', true)->get());
    }

    // 支付积分订单
    public function pay(IntegralOrderService $ios){
        $rs = $ios->createOrder();
        return $rs['status']?$this->success($rs['data']):$this->error($rs['msg']);
    }
}
